<CFPARAM NAME="sMess" default="">
<CFTRANSACTION>
	<CFTRY>
		<CFSET tmpIdDet = "#GetEntry.WSNUMBER#">
		<CFMODULE TEMPLATE="#client.common_path#cfm_DBdateFormat.cfm" dateConvert="#DATEFORMAT(NOW(),'mm/dd/yyyy')#">
		<CFSET sDate = CreateODBCDate(Now())>
		<CFQUERY NAME="getCINWSSUBSKEDPOOL" DATASOURCE="#client.company_dsn#">
			SELECT * FROM CINWSSUBSKEDPOOL
			WHERE WSNUMBER_FK = '#tmpIdDet#'
				AND PATTERNCODE!='' AND PATTERNCODE IS NOT NULL
			ORDER BY PERSONNELIDNO,REFERENCEDATE
		</CFQUERY>

		<CFIF getCINWSSUBSKEDPOOL.RECORDCOUNT>
			<CFLOOP QUERY="getCINWSSUBSKEDPOOL">
				<CFQUERY NAME="verifySubSked" DATASOURCE="#client.company_dsn#">
					SELECT REFERENCEDATE FROM CINWSSUBSKED
					WHERE REFERENCEDATE = '#REFERENCEDATE#'
						AND PERSONNELIDNO='#PERSONNELIDNO#'
				</CFQUERY>
                <CFSET rDate = CreateODBCDate(getCINWSSUBSKEDPOOL.REFERENCEDATE)>    
				<CFIF verifySubSked.RECORDCOUNT>
                
					<CFQUERY NAME="updateSubSked" DATASOURCE="#client.company_dsn#">
						UPDATE CINWSSUBSKED
						SET PATTERNCODE='#PATTERNCODE#',
							CATEGORY='#CATEGORY#',
							USERID='#USERID#',
							DATELASTUPDATE=#sDate#,
							TIMELASTUPDATE='#TimeFormat(NOW(),"HH:mm:ss")#'
						WHERE
                        	REFERENCEDATE = #rDate#
							AND PERSONNELIDNO='#PERSONNELIDNO#'
					</CFQUERY>
				<CFELSE>
					<CFQUERY NAME="insertSubSked" DATASOURCE="#client.company_dsn#">
						INSERT INTO CINWSSUBSKED(PERSONNELIDNO,REFERENCEDATE,PATTERNCODE,CATEGORY,GUID,
							RECCREATEDBY,RECDATECREATED,USERID,DATELASTUPDATE,TIMELASTUPDATE)
						VALUES ('#PERSONNELIDNO#',#rDate#,'#PATTERNCODE#','#CATEGORY#','#GUID#',
							'#RECCREATEDBY#',#sDate#,'#USERID#',#sDate#,'#TimeFormat(NOW(),"HH:mm:ss")#')
					</CFQUERY>
				</CFIF>
			</CFLOOP>
		</CFIF>
		<CFQUERY NAME="getCINWSMASTERPOOL" DATASOURCE="#client.company_dsn#">
			SELECT * FROM CINWSMASTERPOOL
			WHERE WSNUMBER_FK = '#tmpIdDet#'
				AND WORKSKEDCODE!='' AND WORKSKEDCODE IS NOT NULL
			ORDER BY PERSONNELIDNO,STARTDATE
		</CFQUERY>
		<CFIF getCINWSMASTERPOOL.RECORDCOUNT>
			<CFLOOP QUERY="getCINWSMASTERPOOL">
				<CFQUERY NAME="verifyMaster" DATASOURCE="#client.company_dsn#">
					SELECT STARTDATE FROM CINWSMASTER
					WHERE STARTDATE = '#STARTDATE#'
						AND PERSONNELIDNO='#PERSONNELIDNO#'
				</CFQUERY>
				<CFIF getCINWSMASTERPOOL.ACTIVESKED EQ "Y">
					<CFQUERY NAME="deactivateMaster" DATASOURCE="#client.company_dsn#">
						UPDATE CINWSMASTER
						SET 	ACTIVESKED='N'
						WHERE	PERSONNELIDNO='#PERSONNELIDNO#'
					</CFQUERY>
				</CFIF>
				<CFIF verifyMaster.RECORDCOUNT>
					<CFQUERY NAME="updateMaster" DATASOURCE="#client.company_dsn#">
						UPDATE CINWSMASTER
						SET 	ACTIVESKED='#ACTIVESKED#',
							CATEGORY='#CATEGORY#',
							WORKSKEDCODE='#WORKSKEDCODE#',
							STARTDATE='#STARTDATE#',
							GUID='#GUID#',
							USERID='#USERID#',
							DATELASTUPDATE=#sDate#,
							TIMELASTUPDATE='#TimeFormat(NOW(),"HH:mm:ss")#'
						WHERE 	STARTDATE = '#STARTDATE#'
							AND PERSONNELIDNO='#PERSONNELIDNO#'
					</CFQUERY>
				<CFELSE>
					<CFQUERY NAME="insertMaster" DATASOURCE="#client.company_dsn#">
						INSERT INTO CINWSMASTER(PERSONNELIDNO,ACTIVESKED,CATEGORY,WORKSKEDCODE,STARTDATE,GUID,REQUIREHOURSWORK,POLICYCODE,
							RECCREATEDBY,RECDATECREATED,USERID,DATELASTUPDATE,TIMELASTUPDATE)
						VALUES ('#PERSONNELIDNO#','#ACTIVESKED#','#CATEGORY#','#WORKSKEDCODE#','#STARTDATE#','#GUID#','#REQUIREHOURSWORK#','#POLICYCODE#',
							'#RECCREATEDBY#',#sDate#,'#USERID#',#sDate#,'#TimeFormat(NOW(),"HH:mm:ss")#')
					</CFQUERY>
				</CFIF>
			</CFLOOP>
		</CFIF>
		<CFQUERY NAME="getCINWSTEMPSKEDPOOL" DATASOURCE="#client.company_dsn#">
			SELECT * FROM CINWSTEMPSKEDPOOL
			WHERE WSNUMBER_FK = '#tmpIdDet#'
				AND WORKSKEDCODE!='' AND WORKSKEDCODE IS NOT NULL
			ORDER BY PERSONNELIDNO,STARTDATE
		</CFQUERY>
		<CFIF getCINWSTEMPSKEDPOOL.RECORDCOUNT>
			<CFLOOP QUERY="getCINWSTEMPSKEDPOOL">
				<CFQUERY NAME="verifyTempSked" DATASOURCE="#client.company_dsn#">
					SELECT STARTDATE FROM CINWSTEMPSKED
					WHERE STARTDATE = '#STARTDATE#'
						AND PERSONNELIDNO='#PERSONNELIDNO#'
				</CFQUERY>
				<CFIF verifyTempSked.RECORDCOUNT>
					<CFQUERY NAME="updateSubSked" DATASOURCE="#client.company_dsn#">
						UPDATE CINWSTEMPSKED
						SET 	WORKSKEDCODE='#WORKSKEDCODE#',
							STARTDATE='#STARTDATE#',
							ENDDATE='#ENDDATE#',
							CATEGORY='#CATEGORY#',
							GUID='#GUID#',
							USERID='#USERID#',
							DATELASTUPDATE=#sDate#,
							TIMELASTUPDATE='#TimeFormat(NOW(),"HH:mm:ss")#'
						WHERE	STARTDATE = '#STARTDATE#'
							AND PERSONNELIDNO='#PERSONNELIDNO#'
					</CFQUERY>
				<CFELSE>
					<CFQUERY NAME="insertSubSked" DATASOURCE="#client.company_dsn#">
						INSERT INTO CINWSTEMPSKED(PERSONNELIDNO,WORKSKEDCODE,STARTDATE,ENDDATE,CATEGORY,GUID,
							RECCREATEDBY,RECDATECREATED,USERID,DATELASTUPDATE,TIMELASTUPDATE)
						VALUES ('#PERSONNELIDNO#','#WORKSKEDCODE#','#STARTDATE#','#ENDDATE#','#CATEGORY#','#GUID#',
							'#RECCREATEDBY#',#sDate#,'#USERID#',#sDate#,'#TimeFormat(NOW(),"HH:mm:ss")#')
					</CFQUERY>
				</CFIF>
			</CFLOOP>
		</CFIF>
		<CFQUERY NAME="updateAPPROVEDPOST" DATASOURCE="#client.company_dsn#">
			UPDATE CINWORKSCHED
			SET APPROVED = 'Y',
			DATEACTIONWASDONE= #sDate#,
			ACTIONBY='#CLIENT.USERID#',
			DATELASTUPDATE = #sDate#,
			POSTED = 'Y',
			USERID = '#client.chapa#'
			WHERE WSNUMBER = '#tmpIdDet#'
		</CFQUERY>
		<CFSET sMess=sMess & "<br> eWorksked #tmpIdDet# successfully posted">
		<CFCATCH TYPE="ANY">
			<CFSET sWarn = #CFCATCH.message# & #CFCATCH.DETAIL#>
		</CFCATCH>
	</CFTRY>
</CFTRANSACTION>
